﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.Common;
using DevExpress.XtraEditors;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using LocalService = Sunboxsoft.ASMS.Client.Domain.LocalService;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Create
{
    /// <summary>
    /// 新增车辆
    /// </summary>
    public partial class CreateAutomobileView : UserControl, IView
    {
        WebRef.VCustomer model = null;
        WebRef.ECustomer newModel = null;
        Promise _promise = new Promise();

        public CreateAutomobileView()
        {
            InitializeComponent();
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(txtCarOwnerName, "车主姓名", true));
            ValidationManager.Register(this, new Common.Validations.TextEditTelephoneValidation(txtCarOwnerTel, true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(txtCarLPN, "车牌号", false));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(txtCarVIN, "车架号", true));
            ValidationManager.Register(this, new Common.Validations.TextEditIsNotNullValidation(txtEIN, "发动机编号", true));
            ValidationManager.Register(this, new Common.Validations.SearchLookUpEditValidation(searchLookUpCarBrand, "汽车品牌"));
        }
        public CreateAutomobileView(WebRef.VCustomer model)
            : this()
        {
            this.model = model;
        }
        public CreateAutomobileView(WebRef.ECustomer model)
            : this()
        {
            this.newModel = model;
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", Resources.isOK, "保存", OnBtnSave_Click))
                .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnBtnCancel_Click));
        }
        public void Active(ViewActiveArgs args)
        {
            if (model != null)
            {
                this.txtCustomerID.Text = model.Number;
                this.txtCustomerName.Text = model.Name;
                this.txtCustomerPhone.Text = model.Telephone;
                this.txtCarOwnerName.Text = model.Name;
                this.txtCarOwnerTel.Text = model.Telephone;
            }
            if (newModel != null)
            {
                this.txtCustomerID.Text = newModel.Number.ToString();
                this.txtCustomerName.Text = newModel.Name;
                this.txtCustomerPhone.Text = newModel.Telephone;
                this.txtCarOwnerName.Text = newModel.Name;
                this.txtCarOwnerTel.Text = newModel.Telephone;
            }
            SearchLookUpEditUtil.BindEMasterDataItem(this.searchLookUpCarBrand,
                _promise,
                Domain.Constant.MasterDataCodes.AutomobileBrand,
                null,
                null,
                true,
                true);
            SearchLookUpEditUtil.BindEMasterDataItem(this.searchLookUpInsuranceCompany,
                _promise,
                Domain.Constant.MasterDataCodes.InsuranceCompany,
                null,
                null,
                true,
                true);
        }
        public void Resume(ViewResumeArgs args)
        {

        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("新增车辆");
        }
        #endregion

        #region Method
        /// <summary>
        /// 添加车辆信息
        /// </summary>
        private void CreateAutomobile()
        {
            WebRef.EAutomobile modelMobile = new WebRef.EAutomobile();
            if (model != null)
            {
                modelMobile.CustomerId = model.Id;
            }
            if (newModel != null)
            {
                modelMobile.CustomerId = newModel.Id;
            }
            modelMobile.Owner = this.txtCarOwnerName.Text.Trim();
            modelMobile.OwnerTelephone = this.txtCarOwnerTel.Text.Trim();
            modelMobile.LPN = this.txtCarLPN.Text.Trim();
            modelMobile.VIN = this.txtCarVIN.Text.Trim();
            modelMobile.EIN = this.txtEIN.Text.Trim();
            modelMobile.EngineType = this.txtEngineType.Text.Trim();
            modelMobile.Brand = GetSelectedMasterDataItem(this.searchLookUpCarBrand);
            modelMobile.Maker = GetSelectedMasterDataItem(this.searchLookUpCarMarker);
            modelMobile.Series = GetSelectedMasterDataItem(this.searchLookUpSeries);
            modelMobile.Model = GetSelectedMasterDataItem(this.searchLookUpModule);
            if (this.txtBoughtDate.EditValue != null)
            {
                modelMobile.BoughtDate = Convert.ToDateTime(this.txtBoughtDate.EditValue);
            }
            modelMobile.InsuranceCompany = GetSelectedMasterDataItem(this.searchLookUpInsuranceCompany);
            if (!string.IsNullOrEmpty(this.txtInsuranceExpire.Text.Trim()))
            {
                modelMobile.InsuranceExpirationDate = Convert.ToDateTime(this.txtInsuranceExpire.Text.Trim());
            }
            modelMobile.InsuranceNumber = this.txtInsuranceNum.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtNextTime.Text.Trim()))
            {
                modelMobile.NextAnnualSurveyDate = Convert.ToDateTime(this.txtNextTime.Text.Trim());
            }
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.CreateService().CreateAutomobile(modelMobile);
                this.BeginInvoke(new Action(() =>
                {
                    this.HideMask();
                    if (result.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                    }
                }));
            });
        }
        /// <summary>
        /// 获取选择的主数据
        /// </summary>
        /// <returns></returns>
        private string GetSelectedMasterDataItem(SearchLookUpEdit searchLookUpEdit)
        {
            WebRef.EMasterDataItem item = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(searchLookUpEdit);
            if (item == null)
            {
                return null;
            }
            else
            {
                return item.Code;
            }
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBtnSave_Click(object sender, object e)
        {
            string message = null;
            if (ValidationManager.Validate(this, out message))
            {
                CreateAutomobile();
            }
            else
            {
                UIContext.Current.ViewManager.ShowError(message);
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBtnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// 根据品牌获取厂商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpCarBrand_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEditUtil.BindEMasterDataItem(this.searchLookUpCarMarker,
                _promise,
                Domain.Constant.MasterDataCodes.AutomobileMaker,
                GetSelectedMasterDataItem(this.searchLookUpCarBrand),
                null, true, true);
        }
        /// <summary>
        /// 根据厂商获取车系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpCarMarker_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEditUtil.BindEMasterDataItem(this.searchLookUpSeries,
                _promise,
                Domain.Constant.MasterDataCodes.AutomobileSeries,
                GetSelectedMasterDataItem(this.searchLookUpCarMarker),
                 null, true, true);
        }
        /// <summary>
        /// 根据车系获取车型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpSeries_EditValueChanged(object sender, EventArgs e)
        {
            SearchLookUpEditUtil.BindEMasterDataItem(this.searchLookUpModule,
                _promise,
                Domain.Constant.MasterDataCodes.AutomobileModel,
                GetSelectedMasterDataItem(this.searchLookUpSeries),
                 null, true, true);
        }
        #endregion
    }
}
